package com.wiscamp.ninechapters.problems.adapter.resources.sets;


import com.wiscamp.ninechapters.problems.application.dto.commands.sets.CreateRegroupRuleCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.sets.UpdateRegroupRuleCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.sets.RegroupRuleVO;
import com.wiscamp.ninechapters.problems.application.services.RegroupRuleAppService;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.BaseAppService;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 组题规则服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")

@Tag(name = "题库-组题规则信息")
public class RegroupRuleController extends BaseAppService {

    private final RegroupRuleAppService regroupRuleAppService;

    public RegroupRuleController(RegroupRuleAppService regroupRuleAppService) {
        this.regroupRuleAppService = regroupRuleAppService;
    }


    /**
     * 通过规则编号获取组题规则
     *
     * @param request
     * @param response
     * @param ruleId
     * @return
     */
    @GetMapping("RegroupRules/{ruleId}")
    @Operation(summary = "通过规则编号获取组题规则")
    @HandleResourceException
    public RestResponse<RegroupRuleVO> getRegroupRule(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                      @PathVariable("ruleId") long ruleId) {
        return ResourceUtils.getById(request, response, ruleId, regroupRuleAppService::getRegroupRule);
    }

    /**
     * 通过机构编号获取组题规则列表
     *
     * @param organizationId
     * @return 组题规则列表
     */
    @GetMapping("Organizations/{organizationId}/RegroupRules")
    @Operation(summary = "通过机构编号获取组题规则列表")
    @HandleResourceException
    public RestResponse<Collection<RegroupRuleVO>> getRegroupRulesByOrganization(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                 @PathVariable("organizationId") long organizationId) {
        return ResourceUtils.getListById(request, response, organizationId, regroupRuleAppService::getRegroupRulesByOrganization);
    }

    /**
     * 通过用户编号获取组题规则列表
     *
     * @param creatorId
     * @return 组题规则列表
     */
    @GetMapping("Users/{creatorId}/RegroupRules")
    @Operation(summary = "通过用户编号获取组题规则列表")
    @HandleResourceException
    public RestResponse<Collection<RegroupRuleVO>> getRulesByCreator(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                     @PathVariable("creatorId") long creatorId) {
        return ResourceUtils.getListById(request, response, creatorId, regroupRuleAppService::getRulesByCreator);
    }

    /**
     * 通过机构编号和组题规则代码获取组题规则
     *
     * @param ruleCode
     * @return
     */
    @GetMapping("Organizations/{organizationId}/RegroupRules/Code/{ruleCode}")
    @Operation(summary = "通过机构编号和组题规则代码获取组题规则")
    @HandleResourceException
    public RestResponse<RegroupRuleVO> getRuleByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                     @PathVariable("organizationId") long organizationId,
                                                     @PathVariable("ruleCode") String ruleCode) {
        return ResourceUtils.getByTextAndId(request, response, ruleCode, organizationId, regroupRuleAppService::getRegroupRuleByCode);
    }


    /**
     * 通过机构编号和组题规则名称获取组题规则列表
     *
     * @param ruleName
     * @return
     */
    @GetMapping("Organizations/{organizationId}/RegroupRules/Name/{ruleName}")
    @Operation(summary = "通过机构编号和组题规则名称获取组题规则列表")
    @HandleResourceException
    public RestResponse<Collection<RegroupRuleVO>> getRulesByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                  @PathVariable("organizationId") long organizationId,
                                                                  @PathVariable("ruleName") String ruleName) {
        return ResourceUtils.getListByTextAndId(request, response, ruleName, organizationId, regroupRuleAppService::getRegroupRulesByName);
    }

    @GetMapping("Organizations/{organizationId}/RegroupRules/Code/Like/{ruleCode}")
    @Operation(summary = "通过机构编号和组题规则代码获取组题规则列表")
    @HandleResourceException
    public RestResponse<Collection<RegroupRuleVO>> getRegroupRulesByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                  @PathVariable("organizationId") long organizationId,
                                                                  @PathVariable("ruleCode") String ruleCode) {
        return ResourceUtils.getListByTextAndId(request, response, ruleCode, organizationId, regroupRuleAppService::getRegroupRulesByCode);
    }

    /**
     * 通过规则编号删除机构组题规则
     *
     * @param id
     * @return
     */
    @PutMapping("RegroupRules/Actions/Delete")
    @Operation(summary = "通过规则编号删除机构组题规则")
    @HandleResourceException
    public RestResponse<Boolean> deleteRegroupRule(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                   @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, regroupRuleAppService::deleteRegroupRule);
    }

    @DeleteMapping("RegroupRules/{ruleId}")
    @Operation(summary = "通过规则编号删除机构组题规则")
    @HandleResourceException
    public RestResponse<Boolean> deleteRegroupRuleById(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                   @PathVariable("ruleId") long ruleId) {
        var command = new DeleteCommand();
        command.setId(ruleId);
        return ResourceUtils.delete(request, response, command, regroupRuleAppService::deleteRegroupRule);
    }

    /**
     * 创建组题规则
     *
     * @param command
     * @return
     */
    @PostMapping("RegroupRules")
    @Operation(summary = "创建组题规则")
    @HandleResourceException
    public RestResponse<RegroupRuleVO> createRegroupRule(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                         @RequestBody CreateRegroupRuleCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, regroupRuleAppService::createRegroupRule);
    }

    /**
     * 更新组题规则
     *
     * @param command
     * @return
     */
    @PutMapping("RegroupRules")
    @Operation(summary = "更新组题规则")
    @HandleResourceException
    public RestResponse<RegroupRuleVO> updateRegroupRule(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                         @RequestBody UpdateRegroupRuleCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, regroupRuleAppService::updateRegroupRule);
    }


    /**
     * 分页获取组题规则列表
     *
     * @return 组题规则列表
     */
    @PostMapping("RegroupRules/Query")
    @Operation(summary = "分页获取组题规则列表")
    @HandleResourceException
    public RestResponse<Collection<RegroupRuleVO>> getRegroupRulesByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                          @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, regroupRuleAppService::getRegroupRulesByQuery);
    }

    @GetMapping("RegroupRules/SpecialPractice/{gradeLevel}")
    @Operation(summary = "分级别获取获取专项练习组题规则列表")
    @HandleResourceException
    public RestResponse<Collection<RegroupRuleVO>> getSpecialPracticeRules(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                           @PathVariable("gradeLevel") String gradeLevel) {
        var rules = regroupRuleAppService.getSpecialPracticeRules(gradeLevel);
        return ResourceUtils.getOkResponse(request, response, rules);
    }
}
